package cn.lf.android.lawfirm.undertake.apply.view;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.apply.ApplyUndertakeActivity;
import cn.lf.android.lawfirm.undertake.apply.ApplyUndertakeInfoActivity;
import cn.lf.android.lawfirm.undertake.approval.UndertakeActivity;

import static cn.lf.android.lawfirm.R.id.creat_people;
import static cn.lf.android.lawfirm.R.id.head_left;
import static cn.lf.android.lawfirm.R.id.showTimeStr;

public class ApplyUndertakeAdapter extends BaseAdapter {
    private List<LawCase> list;
    private LayoutInflater inflater;
    private Activity context;
    private int mPosition = 0;
    int width = 0;
    /**
     * 0-承办审批
     **/
    private int type;

    public ApplyUndertakeAdapter(Activity context, List<LawCase> list, int type) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_law_distribution, null);
            holder.names = (TextView) convertView
                    .findViewById(R.id.names);
            holder.team = (TextView) convertView
                    .findViewById(R.id.team);
            holder.creat_people = (TextView) convertView
                    .findViewById(creat_people);
            holder.right_two = (TextView) convertView
                    .findViewById(R.id.right_two);
            holder.right_one = (TextView) convertView
                    .findViewById(R.id.right_one);
            holder.head_right = (TextView) convertView
                    .findViewById(R.id.head_right);
            holder.head_left = (TextView) convertView
                    .findViewById(head_left);
            holder.showTimeStr = (TextView) convertView
                    .findViewById(showTimeStr);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LawCase bean = list.get(position);

        holder.names.setText("案件名称：" + bean.caseName);
        holder.team.setText("承办团队：" + bean.members);
        holder.creat_people.setText("创建人：" + bean.createUser);
        holder.head_left.setText("合同号：" + bean.cognizanceNum);
        holder.showTimeStr.setText(bean.showTimeStr);
        if(null==bean.showTimeStr||bean.showTimeStr.length()==0){
            holder.showTimeStr.setVisibility(View.GONE);
            holder.showTimeStr.setText("");
        }else{
            holder.showTimeStr.setVisibility(View.VISIBLE);
            holder.showTimeStr.setText(bean.showTimeStr);
        }
        holder.head_right.setText(bean.state);
        holder.right_two.setVisibility(View.GONE);
        holder.right_one.setText(bean.buttonName);
        holder.right_one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("查看".equals(bean.buttonName)) {
                    Intent intent = new Intent(context, ApplyUndertakeInfoActivity.class);
                    intent.putExtra("sid", list.get(position).sid);
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, ApplyUndertakeActivity.class);
                    intent.putExtra("caseID", list.get(position).caseID);
                    context.startActivity(intent);
                }

            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView head_left, head_right, names, team, creat_people, right_two, right_one,showTimeStr;
    }

}
